using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

/// <summary>
/// Summary description for mailClass
/// </summary>
public class bzMailClass
{

    private static string _Host = ConfigurationManager.AppSettings["SMTP"];
    public static string Host { get { return _Host; } }
    private static string sHost = ConfigurationManager.AppSettings["smtphost"];
    private static string sUserName = ConfigurationManager.AppSettings["username"];
    private static string sPassword = ConfigurationManager.AppSettings["emailpwd"];
    private static string sFromName = ConfigurationManager.AppSettings["sender"];
    private static string sFromEmail = ConfigurationManager.AppSettings["fromemail"];
    private static string sPort = ConfigurationManager.AppSettings["smtpport"];
    public static bool sendMail(string strFrom, string strTo, string strSubject, string strBody, string strMailReply, string strMailSender)
    {
        MailMessage ms = new MailMessage(strFrom, strTo, strSubject, strBody);
        ms.BodyEncoding = System.Text.Encoding.UTF8;
        ms.IsBodyHtml = true;
        ms.ReplyTo = new MailAddress(strMailReply);
        ms.Sender = new MailAddress(strMailSender);
        ms.SubjectEncoding = System.Text.Encoding.UTF8;
        SmtpClient cl = new SmtpClient(_Host);
        try
        {
            cl.Send(ms);
            return true;
        }
        catch
        {
            return SendMail("", strTo, strSubject, strBody, false); ;
        }
    }
    public static bool SendMail(string sToName, string sToEmail, string sHeader, string sMessage, bool fSSL)
    {
        if (sToName.Length == 0)
            sToName = sToEmail;
        if (sFromName.Length == 0)
            sFromName = sFromEmail;

        System.Web.Mail.MailMessage Mail = new System.Web.Mail.MailMessage();

        Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = sHost;
        Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;

        Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = sPort;
        if (fSSL)
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true";

        if (sUserName.Length == 0)
        {
            //Ingen auth 
        }
        else
        {
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = sUserName;
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = sPassword;
        }

        Mail.To = sToEmail;
        Mail.From = sFromEmail;
        Mail.Subject = sHeader;
        Mail.Body = sMessage;
        Mail.BodyFormat = System.Web.Mail.MailFormat.Html;
        Mail.BodyEncoding = System.Text.Encoding.UTF8;

        System.Web.Mail.SmtpMail.SmtpServer = sHost;
        try
        {
            System.Web.Mail.SmtpMail.Send(Mail);
            return true;
        }
        catch { return false; }
    } 
}
